#include "StdAfx.h"
#include "Bus.h"

BusStation::BusStation()
{
	ZeroMemory(&_systemTime, sizeof(_systemTime));
}

BusStation::~BusStation()
{
}

SYSTEMTIME BusStation::getTM() const
{
	return _systemTime;
}

void BusStation::setTM(const SYSTEMTIME &st)
{
	_systemTime = st;
}

Station *BusStation::get()
{
	return _station;
}

void BusStation::set(Station *sn)
{
	_station = sn;
}

BusStationMgr::BusStationMgr()
{

}

BusStationMgr::~BusStationMgr()
{
	clear();
}

void BusStationMgr::enter(Station *sn)
{
	BusStation *bs = NULL;

	if (_busStations.size() >= 5)
	{
		bs = _busStations.back();
		_busStations.pop_back();
	}
	else
	{
		bs = new BusStation;
	}

	SYSTEMTIME st;
	GetLocalTime(&st);

	bs->setTM(st);
	bs->set(sn);
}

BusStation *BusStationMgr::get(size_t index)
{
	std::list<BusStation *>::iterator it = _busStations.begin();
	std::advance(it, index);

	if (it == _busStations.end())
	{
		return NULL;
	}

	return *it;
}

void BusStationMgr::clear()
{
	for (std::list<BusStation *>::iterator it = _busStations.begin(); it != _busStations.end(); it++)
	{
		delete *it;
	}
	_busStations.clear();
}

BusRouteMgr::BusRouteMgr()
: _major(NULL),
_minor(NULL),
_direction(NULL)
{

}

BusRouteMgr::~BusRouteMgr()
{

}

Route *BusRouteMgr::getMajor()
{
	return _major;
}

Route *BusRouteMgr::getMinor()
{
	return _minor;
}

void BusRouteMgr::setMajor(Route *mr)
{
	_major = mr;
}

void BusRouteMgr::setMinor(Route *mr)
{
	_minor = mr;
}

void BusRouteMgr::judge()
{

}

Route *BusRouteMgr::direction()
{
	return _direction;
}

Bus::Bus(void)
{

}

Bus::~Bus()
{

}

Database::BusRow *Bus::getDB()
{
	return &_db;
}

void Bus::setDB(const Database::BusRow &br)
{
	_db = br;
}

BusStationMgr *Bus::stationMgr()
{
	return &_stationMgr;
}

BusRouteMgr *Bus::routeMgr()
{
	return &_routeMgr;
}

//BusMgr
BusMgr::BusMgr()
{
}

BusMgr::~BusMgr()
{
	destory();
}

Bus *BusMgr::create()
{
	Bus *bs = new Bus;

	_buses.push_back(bs);

	for (std::list<IBusMgrObserver *>::iterator it = _observers.begin(); it != _observers.end(); it++)
	{
		IBusMgrObserver *rmo = *it;

		rmo->onCreate(this, bs);
	}

	return bs;
}

void BusMgr::destory(Bus *bs)
{
	_buses.remove(bs);

	delete bs;

	for (std::list<IBusMgrObserver *>::iterator it = _observers.begin(); it != _observers.end(); it++)
	{
		IBusMgrObserver *rmo = *it;

		rmo->onDestory(this, bs);
	}
}

void BusMgr::destory()
{
	while (_buses.size() != 0)
	{
		destory(_buses.front());
	}
}

Bus *BusMgr::get(size_t index)
{
	std::list<Bus *>::iterator it = _buses.begin();
	std::advance(it, index);

	if (it == _buses.end())
	{
		return NULL;
	}

	return *it;
}

Bus *BusMgr::get(const std::string &licencePlate)
{
	for (std::list<Bus *>::iterator it = _buses.begin(); it != _buses.end(); it++)
	{
		Bus *bs = *it;

		if (bs->getDB()->getLicencePlate() == licencePlate)
		{
			return bs;
		}
	}

	return NULL;
}

void BusMgr::addObserver(IBusMgrObserver *rmo)
{
	_observers.push_back(rmo);
}

void BusMgr::delObserver(IBusMgrObserver *rmo)
{
	_observers.remove(rmo);
}